<?php

    /**
     * @package      Modules
     * @subpackage   Core_Classes
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Core_Classes_ErrorListener extends Core_Event_Listener
    {

    ////////////////////////////////////////////////////////////////////////////
    //                                 Methods                                //
    ////////////////////////////////////////////////////////////////////////////

       /**
        * Handles the logError event
        *
        * @param        Core_Event      $event
        * @return       void
        */
        public function onLogError(Core_Event $event)
        {
            $exception = $event->getParam('exception');

            if ($exception === null || !$exception instanceof Exception) {
                return;
            }

            $errorLogPath = Core_IO_Path::combine(
                                                    PROJECT_TEMP_PATH,
                                                    'log_error.txt'
                                                 );

            $now    = new Zend_Date();

            $log    = DbConfiguration::getValue('SITE_NAME') .' @ '. $now->get(Zend_Date::ISO_8601) . "\n";
            $log   .= '------------------------------------------------------------' . "\n";
            $log   .= 'Code : '. $exception->getCode() . "\n";
            $log   .= 'File : '. $exception->getFile() . "\n";
            $log   .= 'Line : '. $exception->getLine() . "\n";
            $log   .= 'Message : '. $exception->getMessage() . "\n";
            $log   .= 'Trace : '. $exception->getTraceAsString() . "\n";
            $log   .= "\n";

            /*
             * Admin mail
             */
            $adminMail = new Core_Mail_Zend('backend');
            $adminMail->setTemplate('log');
            $adminMail->setSubject('['. DbConfiguration::getValue('SITE_NAME') .'] '. get_class($exception));

            $adminMail->setParam('exception', $log);

            $adminList = Doctrine_Query::create()
                            ->select('a.id, a.email')
                            ->from('DbAdmin a')
                            ->leftJoin('a.DbProfile p')
                            ->where('p.slug = ?', DbProfile::TECHNICAL_ADMIN)
                            ->addWhere('status = ?', DbBase::ENABLED)
                            ->execute(array(), Doctrine::HYDRATE_ARRAY);

            $send = false;
            foreach ($adminList as $admin) {
                $adminMail->addTo($admin['email']);
                $send = true;
            }

            if ($send) {
                $adminMail->send();
            }

            if (!Core_IO_File::exists($errorLogPath)) {
                Core_IO_File::writeAll($errorLogPath, $log);

            } else {
                $stream = Core_IO_File::appendText($errorLogPath);
                $stream->write($log);
                $stream->close();
            }
        }
    }
